package com.skt.common_hr.common.approval
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ApprovalLineProxy extends CommonProxy
	{
		public static const NAME:String = "ApprovalLineProxy";
		
		public static const LOAD_APPROVAL_LINE_COMPLETE:String = "loadApprovalLineComplete";
		public static const LOAD_APPROVAL_LINE_FAILED:String = "loadApprovalLineFailed";
		
		public var lineList:ArrayCollection = new ArrayCollection;
		public var bEditable:Boolean = true;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ApprovalLineProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		/**
		 * 초기 신청현황 리스트
		 */
		public function loadApprovalLine(pernr:String, type:String, key:String="", sAwart:String=""):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_PROCGBN"] = type;
						  
			// 그룹웨어키가 (현재는 휴가시만) 넘어오면 ...
			if(type == "01" && key != "" && Variables.company=='NOS') {
				param["I_REQNR"] = key;
			}
			//Alert.show("sAwart :"+sAwart);
			//20130122
			param["I_AWART"] = sAwart;			
			loadData("Z_HR_0108", param, resultLoadLine, faultLoadLine);
			 
		}
		
		public function loadApprovalLine_preOrder(pernr:String, type:String, obj:Object):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_PROCGBN"] = type;
			 
			param["I_BEGDA"] = obj.BEGDA;
			param["I_ENDDA"] = obj.ENDDA;
			param["I_AWART"] = obj.AWART;
			
			loadData("Z_HR_0108A", param, resultLoadLine, faultLoadLine); 
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 신청현황 리스트 조회 성공
		 */
		public function resultLoadLine(event:ResultEvent):void
		{
			trace("resultLoadLine");
			if(retCode==0) {  
				lineList = parseApprovalLine(event.result["ITAB"]);
				bEditable = event.result["FLG"]!="X";
				facade.sendNotification(LOAD_APPROVAL_LINE_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_APPROVAL_LINE_FAILED, this);
			}
		}
		
		/**
		 * 신청현황 리스트 조회 실패
		 */
		private function faultLoadLine(event:FaultEvent):void
		{
			trace("faultLoadLine");
			facade.sendNotification(LOAD_APPROVAL_LINE_FAILED, this);
		}
		
		
		private function parseApprovalLine(src:ArrayCollection):ArrayCollection
		{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var rec:* in src)
			{
				var vo:ApprovalLineVO = new ApprovalLineVO();
				vo.GWARELEV = rec.GWARELEV;
				vo.WPERNR   = rec.WPERNR;
				vo.ENAME    = rec.ENAME;
				vo.ORGTX    = rec.ORGTX;
				vo.PLANST   = rec.PLANST;
				arr.addItem(vo);
			}
			return arr;
		}
	}
}